package com.szcinda.call;

import com.szcinda.call.repository.TicketDetails;
import com.szcinda.call.service.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping({"order"})
public class TicketController {
    private final TicketDetailsService ticketDetailsService;
    private final ExceptionContext exceptionContext;
    private final UserAccountService userAccountService;

    public TicketController(TicketDetailsService ticketDetailsService, ExceptionContext exceptionContext, UserAccountService userAccountService) {
        this.ticketDetailsService = ticketDetailsService;
        this.exceptionContext = exceptionContext;
        this.userAccountService = userAccountService;
    }

    // 更新站点账号信息
    @PostMapping({"updateSite"})
    public Result<String> updateSite(@RequestBody UserAccountDto data) {
        return userAccountService.update(data);
    }

    // 检查账号内站点信息是否超过4个
    @GetMapping({"checkSite/{userName}"})
    public Result<String> checkSite(@PathVariable String userName, @RequestBody SiteDto siteDto) {
        return userAccountService.check(userName, siteDto);
    }

    // 获取账号站点信息
    @GetMapping({"getSite/{userName}"})
    public Result<List<SiteDto>> getSite(@PathVariable String userName) {
        return Result.success(userAccountService.getSite(userName));
    }

    // 发送心跳
    @PostMapping({"addHeartBreak"})
    public Result<String> addHeartBreak(@RequestBody HeartBreakData data) {
        exceptionContext.refresh(data);
        return Result.success();
    }

    // 关闭程序，移除心跳
    @PostMapping({"removeBreak"})
    public Result<String> removeBreak(@RequestBody HeartBreakData data) {
        exceptionContext.removeBreak(data);
        return Result.success();
    }

    // 检查心跳
    @GetMapping({"check/{userName}"})
    public Result<String> addHeartBreak(@PathVariable String userName) {
        return Result.success(exceptionContext.getHeartBreak(userName));
    }

    // 移除心跳
    @GetMapping({"removeHeartBreak/{userName}"})
    public Result<String> removeHeartBreak(@PathVariable String userName) {
        exceptionContext.removeHeartBreak(userName);
        return Result.success();
    }

    // 查询站点账号异常的队列消息
    @PostMapping({"queryMessage"})
    public Result<String> create(@RequestBody List<String> userNames) {
        return Result.success(exceptionContext.getMessage(userNames));
    }

    // 向异常的队列发送消息
    @PostMapping({"addMessage"})
    public Result<String> addMessage(@RequestBody ExceptionDto exceptionDto) {
        exceptionContext.putMessage(exceptionDto);
        return Result.success();
    }


    // 获取心跳队列消息
    @GetMapping("getMessages")
    public Result<List<ExceptionContext.BreakDto>> getMessages() {
        return Result.success(exceptionContext.getMessages());
    }

    // 获取心跳异常队列消息
    @GetMapping("getErrorMessages")
    public Result<List<String>> getErrorMessages() {
        return Result.success(exceptionContext.getErrorMessages());
    }



    // 向异常的队列清空消息
    @PostMapping({"removeMessage"})
    public Result<String> removeMessage(@RequestBody List<String> userNames) {
        exceptionContext.removeByUserName(userNames);
        return Result.success();
    }

    // 前台、站点创建
    @PostMapping({"create"})
    public Result<String> create(@RequestBody TicketDetailsCreateDto createDto) {
        return Result.success(ticketDetailsService.create(createDto));
    }

    // exe 前端查询
    @PostMapping("query")
    public PageResult<TicketDetails> query(@RequestBody TicketQueryParams params) {
        return ticketDetailsService.query(params);
    }

    // exe 前端查询异常数据
    @PostMapping("queryException")
    public PageResult<TicketDetails> queryException(@RequestBody SearchExceptionDataParams params) {
        return ticketDetailsService.queryExceptionData(params);
    }

    @PostMapping("changeException")
    public Result<String> changeException(@RequestBody List<String> ticketIds) {
        ticketDetailsService.changeException(ticketIds);
        return Result.success();
    }

    // exe 前端查询
    @PostMapping("querySms")
    public PageResult<SmsData> querySms(@RequestBody SmsQueryDto params) {
        return ticketDetailsService.query(params);
    }

    @PostMapping("billQuery")
    public List<BillData> billQuery(@RequestBody BillQueryParams params) {
        return ticketDetailsService.billQuery(params);
    }

    // 查找当前站点前台需要填充备注的数据
    @PostMapping({"getQTData"})
    public Result<List<QianTaiData>> getQTData(@RequestBody SearchQianTaiDataParams params) {
        return Result.success(ticketDetailsService.findQianTaiData(params));
    }


    // 前台完成
    @GetMapping({"finish/{ticketId}"})
    public Result<String> testRedis(@PathVariable String ticketId) {
        ticketDetailsService.finish(ticketId);
        return Result.success();
    }


    // 人工完成 exe 勾选完成
    @PostMapping("artificial")
    public Result<String> artificial(@RequestBody List<String> ticketIds) {
        ticketDetailsService.artificial(ticketIds);
        return Result.success();
    }

    // 测试打电话
    @GetMapping("test/{phone}")
    public Result<String> test(@PathVariable String phone) {
        TicketDetailsCreateDto createDto = new TicketDetailsCreateDto();
        createDto.setBaseCallNumber(phone);
        createDto.setUserName("test");
        createDto.setSite("test");
        createDto.setTrackingNumber("1234567890");
        createDto.setInputGroup("前台组");
        return Result.success(ticketDetailsService.create(createDto));
    }

    // 重新拨打
    @GetMapping("reCall/{userName}/{site}/{trackingNumber}")
    public Result<String> reCall(@PathVariable String userName, @PathVariable String site, @PathVariable String trackingNumber) {
        ticketDetailsService.reCall(userName, site, trackingNumber);
        return Result.success();
    }
}
